//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory
//===============================================================================
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// This class is the concrete implementation of a View in the Model-View-Presenter 
// pattern. Communication between the Presenter and this class is acheived through 
// an interface to facilitate separation and testability.
// Note that the Presenter generated by the same recipe, will automatically be created
// by CAB through [CreateNew] and bidirectional references will be added.
//
// For more information see:
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.scsf.2006jun/SCSF/html/03-030-Model%20View%20Presenter%20%20MVP%20.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using GlobalBank.Infrastructure.Interface.BusinessEntities;
using GlobalBank.Infrastructure.Interface.Services;
using GlobalBank.Infrastructure.UI;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace GlobalBank.BranchSystems.Module.Views
{
	[SmartPart]
	public partial class FindCustomerResultsView : UserControl, IFindCustomerResultsView, ISmartPartInfoProvider
	{
		public FindCustomerResultsView()
		{
			InitializeComponent();
		}

		private IMessageBoxService _messageBoxService;

		/// <summary>
		/// Sets the presenter. The dependency injection system will automatically
		/// create a new presenter for you.
		/// </summary>
		[CreateNew]
		public FindCustomerResultsViewPresenter Presenter
		{
			set
			{
				_presenter = value;
				_presenter.View = this;
			}
		}

		[ServiceDependency]
		public IMessageBoxService MessageBoxService
		{
			set { _messageBoxService = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			_presenter.OnViewReady();
		}

		// ISmartPartInfoProvider implementation

		public ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
		{
			ISmartPartInfo spi;


			if (smartPartInfoType.IsAssignableFrom(typeof (WindowSmartPartInfo)))
			{
				WindowSmartPartInfo wspi = new WindowSmartPartInfo();
				wspi.Modal = true;
				wspi.MinimizeBox = false;
				wspi.MaximizeBox = false;
				wspi.ControlBox = true;

				wspi.Keys[WindowWorkspaceSetting.FormBorderStyle] = FormBorderStyle.FixedDialog;
				wspi.Keys[WindowWorkspaceSetting.AcceptButton] = _queueForServiceButton;
				wspi.Keys[WindowWorkspaceSetting.CancelButton] = _cancelButton;

				spi = wspi;
			}
			else
			{
				spi = Activator.CreateInstance(smartPartInfoType) as ISmartPartInfo;
			}

			spi.Description = Properties.Resources.FindCustomerResultsViewDescription;
			spi.Title = Properties.Resources.FindCustomerResultsViewTitle;

			return spi;
		}

		// User interaction handling

		private void _cancelButton_Click(object sender, EventArgs e)
		{
			_presenter.OnCancel();
			Dispose();
		}		

		private void _queueForServiceButton_Click(object sender, EventArgs e)
		{
			_presenter.AddReasonForVisit((Customer) _customerBindingSource.Current);
		}

		private void _customerBindingSource_CurrentChanged(object sender, EventArgs e)
		{
			_addressBindingSource.Clear();
			_workNumberTextBox.Clear();
			_homePhoneNumberBindingSource.Clear();
			_mobilePhoneNumberBindingSource.Clear();
			_emailAddressBindingSource.Clear();

			if (_customerBindingSource.Current == null)
				return;

			Customer customer = ((Customer) _customerBindingSource.Current);

			if (customer.Addresses != null && customer.Addresses.Length > 0)
				_addressBindingSource.Add(customer.Addresses[0]);

			if (customer.PhoneNumbers != null && customer.PhoneNumbers.Length > 0)
			{
				foreach (PhoneNumber pn in customer.PhoneNumbers)
				{
					if (pn.PhoneType == PhoneType.Home) _homePhoneNumberBindingSource.Add(pn);
					if (pn.PhoneType == PhoneType.Work) _workPhoneNumberBindingSource.Add(pn);
					if (pn.PhoneType == PhoneType.Mobile) _mobilePhoneNumberBindingSource.Add(pn);
				}
			}

			if (customer.EmailAddresses != null && customer.EmailAddresses.Length > 0)
				_emailAddressBindingSource.Add(customer.EmailAddresses[0]);
		}

		// IFindCustomerResultsView implementation

		public void NotifyCustomerAlreadyInQueue()
		{
			_messageBoxService.Show(Properties.Resources.CustomerAlreadyInQueueMessage,
			                        Properties.Resources.CustomerAlreadyInQueueTitle);
		}


		public void ShowResults(Customer[] customers)
		{
			_customerBindingSource.DataSource = customers;
		}
	}
}
